package org.jimm.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

public class CodeReceiver extends BroadcastReceiver {
    public static Context mContext;
    String LOG_TAG = "Jimm:CodeReceiver";
    private static String ACTION = "org.jimm.ACTIONS";
    private static final String ACTIONS = "act";
    private static final int ACTION_EXIT = 1000;
    private static final int ACTION_DISCONNECT_ALL = 0;
    private static final int ACTION_CONNECT_ALL = 999;


    @Override
    public void onReceive(Context context, Intent intent) {
        String intent_action = intent.getAction();
        //preferences = PreferenceManager.getDefaultSharedPreferences(context);
        int CamAct = 0;//Preferences.camAct;
        Log.i(LOG_TAG, "CamAct" + CamAct);
        Log.i(LOG_TAG, "Integer.parseInt(CamAct)" + CamAct);
        if (intent_action.equals("android.provider.Telephony.SECRET_CODE")) {
            //Log.i(LOG_TAG,"context= "+context);
            //Log.i(LOG_TAG,"intent= "+intent);
            if (mContext == null) mContext = context;
            start(context);
            Toast.makeText(context, "Secret! Jimm started! =)", Toast.LENGTH_LONG).show();
        }
        Log.i("Jimm:Receiver", "intent_action= " + intent_action);

        if (intent_action.equals(Intent.ACTION_CAMERA_BUTTON)) {

            KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if (key.getAction() == KeyEvent.ACTION_DOWN) {
                //	Log.i(LOG_TAG,"prefs.getCamAct()"+prefs.getCamAct());
                //	Log.i(LOG_TAG,"Integer.parseInt(prefs.getCamAct())"+Integer.parseInt(prefs.getCamAct()));
                switch (CamAct) {
                    case 1:
                        abortBroadcast();
                        start(context);
                        break; //launch
//                  		case 2: if (JimmApplication.appStarted) {
//                  			 abortBroadcast();
//                  			start(context); 
//                  			} else {
//                  				sendClose(context);
//              	                }
//                  		
                    //           		break; //maximize
                    case 3:
                        sendClose(context);
                        break; //nothing
                    default:
                        start(context);
                        abortBroadcast();
                        break;
                }
                //abortBroadcast();
                Toast.makeText(context, CamAct, Toast.LENGTH_SHORT).show();

            }
        }

        int type = intent.getIntExtra(ACTIONS, ACTION_EXIT);
        switch (type) {
            case ACTION_EXIT:
                Toast.makeText(context, "CloseApp", Toast.LENGTH_SHORT).show();
                Log.i(LOG_TAG, "CloseApp");
                //JimmApplication.getApplication().stopService();
                break;
            case ACTION_DISCONNECT_ALL:

                break;
            case ACTION_CONNECT_ALL:

                break;
        }

        //if (!JimmApplication.appStarted) JimmApplication.closeApp();
    }

    private void start(Context context) {
        Intent i = new Intent(context, org.jimm.app.ContactListActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }

    private void sendClose(Context context) {
        Intent intent = new Intent(ACTION);
        intent.putExtra(ACTIONS, ACTION_EXIT);
        context.sendBroadcast(intent);
    }
}